import defaultImg from '@/assets/images/200.png'
const importFn = require.context('./', false, /\.vue$/)
console.log(importFn.keys());
export default {
    install(Vue){
        importFn.keys().forEach(path => {
            const component = importFn(path).default
            Vue.component(component.name, component)
        });
        defineDirective(Vue)
    }
}
const defineDirective = (Vue) => {
    Vue.directive('lazy', {
        inserted(el, binding) {
            const observe = new IntersectionObserver(([{isIntersecting}]) => {
                if(isIntersecting){
                    observe.unobserve(el)
                    el.src = binding.value
                    el.onerror = () => {
                        el.src = defaultImg
                    }
                }
            }, {
                threshold: 0.01
            })
            observe.observe(el)
        }
    })
}
